import type { ChannelItem, ChannelResData } from '@/types/data'
import axios from 'axios'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

export const useCounterStore = defineStore('counter', () => {
  // 1.响应式数据--相当于vue2中的state
  const count = ref(0)
  // 2.计算属性--- getters
  const doubleCount = computed(() => count.value * 2)
  // 3.操作数据的方法actions/mutations ----- 不分同步异步直接书写
  const add = () => count.value++
  const cut = () => count.value--

  const channelList = ref<ChannelItem[]>([])

  const getChannelList = async () => {
    const res = await axios.get<ChannelResData>('http://geek.itheima.net/v1_0/channels')
    channelList.value = res.data.data.channels
  }
  // 4. return向外暴露
  return { count, doubleCount, add, cut, channelList, getChannelList }
})
